<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="pwdForm"
      :rules="pwdFormRules"
      ref="pwdFormRef"
      label-width="100px"
      status-icon
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('pwdFormRef')"
          >修改密码</el-button
        >
        <el-button @click="resetForm('pwdFormRef')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updateUserPwd } from '@/api/user/index.js'
// 引入 base64 给用户密码加密
const Base64 = require('js-base64').Base64
export default {
  name: 'UserPwd',
  data() {
    // 旧密码的输入内容与登录的密码不一致
    const loginPwd = (rule, value, callback) => {
      // 解密
      const decryptPwd = Base64.decode(this.$store.state.userPwd)
      // console.log(decryptPwd)
      if (value !== decryptPwd) {
        callback(new Error('旧密码输入错误!'))
      } else {
        callback()
      }
    }
    // 新密码不能和旧密码一样
    const validatePass1 = (rule, value, callback) => {
      if (value === this.pwdForm.old_pwd) {
        callback(new Error('新密码不能和旧密码一致!'))
      } else {
        callback()
      }
    }
    // 新密码要与再次输入的新密码一样
    const validatePass2 = (rule, value, callback) => {
      if (value !== this.pwdForm.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '', // 旧密码
        new_pwd: '', // 新密码
        re_pwd: '', // 再次确认新密码
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: loginPwd, trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePass1, trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
        ],
        re_pwd: [
          { required: true, message: '请再次确认新密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateUserPwd(this.pwdForm).then(({ data: res }) => {
            if (res.code !== 0) return this.$message.error('修改用户密码失败')
            this.$message.success(res.message)
            this.$router.push('/login')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
